<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

error_reporting(E_ALL | E_STRICT);

if (DISPLAY_ERRORS == false) {
  ini_set('display_errors', 'off');
}
if (LOG_ERRORS == true) {
  ini_set('log_errors', 'on');
  ini_set('error_log', LOG_ERRORS_FILE);
}

set_error_handler('my_error_handler');

function my_error_handler($errno, $errstring, $errfile, $errline, $errcontext)
{
  if ($errno != E_STRICT && error_reporting() != 0):

  $error = "==  ==  ==  ==\nPHP ERROR\n==  ==  ==  ==\n";
  $error .= "Number: [$errno]\n";
  $error .= "String: [$errstring]\n";
  $error .= "File:   [$errfile]\n";
  $error .= "Line:   [$errline]\n";

  $html = '<div style="border-style: solid; border-color: #FF0000; text-align: left; padding: 1em;">'.str_replace('PHP ERROR', '<font color="red">PHP ERROR</font>', str_replace("\n",'<br>',$error)).'</div>';

  if (DISPLAY_ERRORS == true) {
    echo $html;
  }

  if (SEND_ERROR_MAIL == true) {
    $mail = new MyMailer;
    $mail->From=ERROR_MAIL_FROM;
    $mail->FromName=ERROR_MAIL_FROMNAME;
    $mail->AddAddress(ERROR_MAIL_TO);
    $mail->Subject=ERROR_MAIL_SUBJECT;
    $mail->Body=$error;
    if(!$mail->Send()) { echo "There was an error sending the error mail message"; }
  }

  if (LOG_ERRORS == true) {
    $errLogLine = date("d/m/Y H:i:s")." - [$errno] $errfile:$errline: $errstring \n";
    error_log($errLogLine, 3, LOG_ERRORS_FILE);
    }

  if ($errno == E_USER_ERROR) {
    exit;
  }

  endif;
}

?>